import { createStore, Store, useStore as baseUseStore } from 'vuex'
import { InjectionKey } from 'vue'
// import { IUserInfo } from '@/api/types/common'
// import { setItem, getItem } from '@/utils/storage'
import { setItem } from '@/utils/storage'
import { USER } from '@/utils/constants'

// 为 store state 声明类型
// export interface State {
//   count: number
//   isCollapse: boolean
// }
const state = {
  count: 1,
  isCollapse: false,
  // user1: getItem<{ token: string } & IUserInfo>(USER)
  user: { token: 'Bearer dsfsdgfsdg' }
}
export type State = typeof state

// 定义 injection key
export const key: InjectionKey<Store<State>> = Symbol('store')
export const store = createStore<State>({
  state () {
    return state
  },
  mutations: {
    add (state) {
      state.count++
    },
    setisCollapse (state, paylocad) {
      state.isCollapse = paylocad
    },
    setUser (state, paylocad) {
      state.user = paylocad
      setItem(USER, paylocad)
    }
  }
})
export const useStore = () => {
  return baseUseStore(key)
}
